package com.bjsxt.shop.head.springmvc.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.fastjson.JSONObject;
import com.bjsxt.common.util.ConstatFinalUtil;
import com.bjsxt.shop.goods.pojo.AGoods;
import com.bjsxt.shop.goods.pojo.AUsersCar;
import com.bjsxt.shop.goods.service.IGoodsService;
import com.bjsxt.shop.orders.pojo.AOrders;
import com.bjsxt.shop.orders.pojo.AOrdersGoods;
import com.bjsxt.shop.orders.service.IOrdersService;
import com.bjsxt.shop.users.pojo.AUsers;
import com.bjsxt.shop.users.pojo.AUsersAddress;
import com.bjsxt.shop.users.service.IUsersShopService;

@Controller
@RequestMapping("/head/orders/")
public class OrdersHeadController
{
	@Resource
	private IUsersShopService usersShopService;
	@Resource
	private IGoodsService goodsService;
	@Resource
	private IOrdersService ordersService;
	/**
	 * 订单打开页面
	 * 要查询当前用户所有的已经启用的收货地址
	       要查询在购物车中选择的购物车的id:(除了有商品,还有数量)
	 * @return
	 */
	@RequestMapping("/ordersInsert")
	public String ordersInsert(String[] ucid,HttpSession session,Model model)
	{
		ConstatFinalUtil.SYS_LOGGER.info("-------ordersInsert-------");
		AUsers users=(AUsers) session.getAttribute("users");
		/*
		 * 要查询当前用户所有的已经启用的收货地址
		 * */
		Map<String, Object> condMap=new HashMap<>();
		condMap.put("usersId",users.getId());
		condMap.put("status", "1");
		Map<String, Object> resultMap=this.usersShopService.findCondListUsersAddressService(null, condMap);
		List<AUsersAddress> usersAddressList=(List<AUsersAddress>) resultMap.get("list");
		model.addAttribute("usersAddressList", usersAddressList);
		
		/*
		 * 要查询在购物车中选择的购物车的id:(除了有商品,还有数量)
		 * */
		List<AUsersCar> usersCarList=new ArrayList<AUsersCar>();
		if(ucid!=null)
		{
			for (int i = 0; i < ucid.length; i++)
			{
				String ucidTemp=ucid[i];
				/* UsersCarId:购物车
				 * 根据购物车的id查询购物车
				 *  */
				condMap.clear();
				condMap.put("id", ucidTemp);
				AUsersCar usersCar = this.goodsService.findOneUsersCarService(condMap);
				usersCarList.add(usersCar);
			}
		}
		session.setAttribute("usersCarList", usersCarList);
		return "/head/orders/ordersInsert";
	}
	
	@RequestMapping("/ordersInsertSubmit")
	public String ordersInsertSubmit(HttpServletRequest request,AOrders orders)
	{
		ConstatFinalUtil.SYS_LOGGER.info("---------ordersInsertSubmit---------");
		HttpSession session=request.getSession();
		AUsers users=(AUsers) session.getAttribute("users");
		/* 调用用户id */
		orders.setUsersId(users.getId());
		/* 订单明细:怎么取 
		 * 从Session中取,目的:安全,任何修改了订单添加页面的html,木有用
		 * */
		List<AOrdersGoods>  ordersGoodList=new ArrayList<AOrdersGoods>();
		double totalPrice = 0 ; 
		List<AUsersCar> usersCarList = (List<AUsersCar>) session.getAttribute("usersCarList");
		for (Iterator iterator = usersCarList.iterator(); iterator.hasNext();)
		{
			AUsersCar usersCar = (AUsersCar) iterator.next();
			AGoods goods=usersCar.getGoods();
			/* 需要订单明细 */
			AOrdersGoods ordersGoods=new AOrdersGoods();
			ordersGoods.setGoodsId(usersCar.getGoodsId());
			ordersGoods.setName(goods.getName());
			ordersGoods.setPrice(goods.getPrice());
			ordersGoods.setWeight(goods.getWeight());
			ordersGoods.setNum(usersCar.getNum());
			ordersGoods.setStatus(Byte.valueOf("1"));
			ordersGoods.setCreateTime(new Date());
			ordersGoods.setUpdateTime(new Date());
			/* 总价格 */
			ordersGoods.setTotalPrice(ordersGoods.getPrice() * ordersGoods.getNum());
			ordersGoodList.add(ordersGoods);
			totalPrice += ordersGoods.getTotalPrice();
		}
		/* 订单的总价格 */
		orders.setTotalPrice(totalPrice);
		orders.setOrdersGoodList(ordersGoodList);
		
		orders.setCreateTime(new Date());
		orders.setUpdateTime(new Date());
		orders.setPubTime(new Date());
		JSONObject resDbJSON = this.ordersService.saveOneOrdersService(orders);
		ConstatFinalUtil.SYS_LOGGER.info("----返回结果:{}",resDbJSON);
		/* 一定客户端跳转 
		 * 万恶的Dubbo,取id从返回值中取
		 * */
		String id = "" ; 
		if("0".equalsIgnoreCase(resDbJSON.get("code") + ""))
		{
			JSONObject dataJSON = (JSONObject) resDbJSON.get("data");
			id = dataJSON.get("id") + "";
		}
		return "redirect:/head/orders/ordersInfo.htm?oid=" + id;
	}
	
	/**
	 * 订单详情页面
	 * @return
	 */
	@RequestMapping("/ordersInfo")
	public String ordersInfo(String oid,Model model)
	{
		ConstatFinalUtil.SYS_LOGGER.info("--ordersInfo--");
		/* 按照订单Id查询信息 */
		Map<String, Object> condMap = new HashMap();
		condMap.put("id", oid);
		AOrders orders = this.ordersService.findOneOrdersService(condMap);
		model.addAttribute("orders", orders);
		return "/head/orders/ordersInfo";
	}
}
